package com.course.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * Created by Justin on 2017/5/30.
 */
public class AuthUtils {

    private static final String SECRET = "english-course-secret";

    public static String toToken(String username) {
        return Jwts.builder()
                .setSubject(username)
                .setExpiration(genExpiration())
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .compact();
    }

    public static String parseToken(String token) {
        return Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token)
                .getBody().getSubject();
    }

    private static Date genExpiration() {
        return DateUtils.toDate(LocalDateTime.now().plusDays(10));
    }
}
